<template>
  <div class="register">
    <el-row justify="center" align="middle" style="height: 100vh;">
      <el-col :span="4" :xs="18" :sm="12" :md="8" :lg="6" :xl="4"> <!-- 进一步减小宽度 -->
        <el-card shadow="always" class="register-card">
          <el-form :model="form" :rules="rules" size="small" label-position="top" ref="registerForm"> <!-- 使用 small 尺寸 -->
            <el-form-item label="用户名:" prop="username">
              <el-input v-model="form.username" type="text" placeholder="请输入用户名" clearable>
                <template #prepend><el-button :icon="UserFilled"></el-button></template>
              </el-input>
            </el-form-item>
            <el-form-item label="密码:" prop="pwd">
              <el-input v-model="form.pwd" type="password" placeholder="请输入密码" show-password clearable>
                <template #prepend><el-button :icon="Lock"></el-button></template>
              </el-input>
            </el-form-item>
            <el-form-item label="真实姓名:" prop="real_name">
              <el-input v-model="form.real_name" placeholder="请输入真实姓名" clearable></el-input>
            </el-form-item>
            <el-form-item label="性别:" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别" clearable>
                <el-option label="男" value="M"></el-option>
                <el-option label="女" value="F"></el-option>
              </el-select>
            </el-form-item>
            <el-row :gutter="8"> <!-- 减小按钮间距 -->
              <el-col :span="12">
                <el-form-item>
                  <el-button type="success" @click="handleRegister" class="full-width">注册</el-button>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item>
                  <el-button type="primary" @click="resetForm" class="full-width">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="8"> <!-- 减小按钮间距 -->
              <el-col :span="24">
                <el-form-item>
                  <el-button type="info" @click="goToLogin" class="full-width">返回登录</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang='ts'>
import { ref, reactive } from "vue";
import { UserFilled, Lock } from "@element-plus/icons-vue"; // 如果需要其他图标，记得导入
import { registerApi } from '@/api/userApi'; // 请根据实际路径调整
import { useRouter } from "vue-router";
import CryptoJS from 'crypto-js'; // 引入加密模块

let form = reactive({
  username: '',
  pwd: '',
  real_name: '',
  sex: ''
});

let registerForm = ref();
let router = useRouter();

// 校验规则
let rules = reactive({
  username: [
    {
      required: true,
      message: '请输入用户名',
      trigger: 'blur'
    }
  ],
  pwd: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }
  ],
  real_name: [
    {
      required: true,
      message: '请输入真实姓名',
      trigger: 'blur'
    }
  ],
  sex: [
    {
      required: true,
      message: '请选择性别',
      trigger: 'change'
    }
  ]
});

// 提交注册表单
async function handleRegister() {
  registerForm.value.validate(async (valid: boolean) => {
    if (!valid) return;

    // 定义密钥
    const secretKey = "AAAABBBBCCCCDDDD"; // 使用与后端一致的密钥

    // 加密密码
    const encryptedPwd = CryptoJS.AES.encrypt(form.pwd, CryptoJS.enc.Utf8.parse(secretKey), {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }).toString();

    // 将加密后的密码替换原密码
    form.pwd = encryptedPwd;

    try {
      await registerApi(form);
      alert('注册成功！'); // 可以进一步处理成功后跳转的逻辑
      resetForm();
    } catch (error) {
      alert('注册失败，请重试！');
    }
  });
}

function resetForm() {
  registerForm.value.resetFields();
}

// 跳转到登录页面
function goToLogin() {
  router.replace('/login'); // 跳转到登录页面的路由
}
</script>

<style scoped>
.register {

    background-color: #EFF3F5;

}

.register-card {
  padding: 12px; /* 进一步减少内边距 */
  border-radius: 8px;
  background-color: rgb(234, 239, 255); /* 半透明背景 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

:deep(.el-form-item__label) {
  color: #000000 !important; /* 确保文本颜色适合新的背景 */
}

.full-width {
  width: 100%;
}

.el-row + .el-row {
  margin-top: 8px; /* 进一步减少行之间的间距 */
}

/* Optional: Customize the input and button styles for a more consistent look */
:deep(.el-input__inner) {
  height: 32px; /* 减小输入框高度 */
  font-size: 12px;
}

:deep(.el-button) {
  height: 32px; /* 减小按钮高度 */
  font-size: 12px;
}

:deep(.el-button--success) {
  background-color: #67c23a;
  border-color: #67c23a;
}

:deep(.el-button--primary) {
  background-color: #409eff;
  border-color: #409eff;
}

:deep(.el-button--info) {
  background-color: #909399;
  border-color: #909399;
}
</style>